package com.beyondprototype.batch.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.data.MongoItemReader;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.FileSystemResource;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import com.beyondprototype.batch.model.CustomerMarker;
import com.beyondprototype.batch.service.CustomerMarkerService;

import lombok.extern.slf4j.Slf4j;

@Configuration
@Slf4j
public class P1PACalculationBatchConfiguration {
   
    @Autowired
    private CustomerMarkerService customerMarkerService;

    @Autowired
    private MongoTemplate mongoTemplate;
    
    private Map<String, List<CustomerMarker>> cache = new HashMap<>();
    
    @Bean
    @Qualifier("p1paItemReader")
    public ItemReader<CustomerMarker> p1paItemReader() {
    	MongoItemReader<CustomerMarker> reader = new MongoItemReader<CustomerMarker>();
    	
    	reader.setTemplate(mongoTemplate);
    	reader.setTargetType((Class<? extends CustomerMarker>) CustomerMarker.class);  
    	
    	//Query query = new Query(Criteria.where("asOfDate").is("2023-10-20").and("status").is("Processing"));
    	Query query = new Query();
    	reader.setQuery(query); 
    	
        Map<String, Direction> sorts = new HashMap<String, Sort.Direction>(1);
        sorts.put("globalIdentifer", Sort.Direction.ASC);
        reader.setSort(sorts);

        return reader;
    }
    

    @Bean 
    @Qualifier("p1paItemWriter")
    public ItemWriter<CustomerMarker> p1paItemWriter() {
        return new ItemWriter<CustomerMarker>() {
            @Override
            public void write(List<? extends CustomerMarker> items) throws Exception {
                for (CustomerMarker item : items) {
                    log.info("Updating "+ item.toString());
                }
                
                //customerMarkerService.update(items);
            }
        };
    }

    @Bean
    @Qualifier("p1paItemProcessor")
    public ItemProcessor<CustomerMarker, CustomerMarker> p1paItemProcessor() {
        return new ItemProcessor<CustomerMarker, CustomerMarker>() {
            @Override
            public CustomerMarker process(CustomerMarker item) throws Exception {
            	
            	log.info("Processing "+item.toString());
            	
            	item.setStatus("Done");
            	
//            	if(cache.isEmpty()) {
//            		
//            	}else {
//            		
//            	}
//            	
//            	List<CustomerMarker> t = cache.get(item.getGlobalIdentifier());
//            	if( t == null) {
//            		t = new ArrayList<>();
//            	}
//            	t.add(item);
            	
                return item;
            }
        };
    }   
}
